package com.school.graduation.detailed.view;

import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.support.v7.widget.CardView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.afollestad.materialdialogs.DialogAction;
import com.afollestad.materialdialogs.MaterialDialog;
import com.bumptech.glide.Glide;
import com.school.graduation.R;
import com.school.graduation.common.base.BaseActivity;
import com.school.graduation.model.Detailed;
import com.school.graduation.util.ToastUtil;

import java.io.IOException;

import butterknife.Bind;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class BuyActivity extends BaseActivity {
    @Bind(R.id.product_img)
    ImageView image;
    @Bind(R.id.leixing)
    TextView leixing;
    @Bind(R.id.detailed)
    TextView product;
    @Bind(R.id.seller_text)
    TextView seller;
    @Bind(R.id.price)
    TextView price;
    @Bind(R.id.buy_layout)
    CardView buy;
    @Bind(R.id.back_button)
    ImageView back;
    private Detailed detailed;
    private String id;
    private MaterialDialog dialog;
    @Override
    public int getContentView() {
        return R.layout.activity_buy;
    }

    @Override
    public void initData() {
        detailed = (Detailed)getIntent().getSerializableExtra("data");
        id = detailed.getProduct().get_id();
        initContent();
    }

    private void initContent(){
        //leixing.setText(detailed.getProduct().getCatogaryName());
        product.setText(detailed.getProduct().getDetail());
        seller.setText(detailed.getProduct().getSellerName());
        price.setText("￥"+detailed.getProduct().getPrice());
        initImage();
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        buy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog = new MaterialDialog.Builder(context)
                        .progress(true,1)
                        .title("请稍后")
                        .content("正在请求")
                        .build();
                dialog.show();
                send();
            }
        });
    }

    private void initImage(){
        if (detailed.getProduct().getImg() != null && !detailed.getProduct().getImg().isEmpty()){
            Glide.with(context)
                    .load(detailed.getProduct().getImg())
                    .into(image);
        }
    }

    private void send(){
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url("http://123.206.20.29:3000/admin/list?id="+id)
                .delete()
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                OnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                        ToastUtil.makeShortToast(context,"请求失败,请稍后再试");
                    }
                });
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //ToastUtil.makeShortToast(context,"下单成功，请线下自行交易");
                OnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss();
                        MaterialDialog dialog = new MaterialDialog.Builder(context)
                                .positiveText("确认")
                                .title("消息")
                                .content("下单成功，点确认退出下单界面")
                                .onPositive(new MaterialDialog.SingleButtonCallback() {
                                    @Override
                                    public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
                                        finish();

                                    }
                                })
                                .show();
                    }
                });
            }
        });

    }

    private void OnUiThread(Runnable runnable){
        new Handler(Looper.getMainLooper()).post(runnable);
    }


}
